package com.tz.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 * Created by heziqian on 2016/10/21.
 */
public class HibernateTemplate {
	static SessionFactory sf = HibernateUtils.getSessionFactory();

	public static final Object execute(HibernateCallback hcb) {
		Session ses = null;
		Transaction tx = null;
		Object result = null;
		try {
			ses = sf.openSession();
			tx = ses.beginTransaction();

			result = hcb.doInHibernate(ses);

			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
			e.printStackTrace();
		} finally {
			if (ses != null) {
				ses.close();
			}
		}
		return result;
	}
}
